package org.bambrikii.examples.rutext.parser;

import java.util.Scanner;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultParser implements IParser {

	private static final Logger logger = LoggerFactory.getLogger(DefaultParser.class);

	private Scanner scanner;
	private String last;

	public DefaultParser(String str) {
		scanner = new Scanner(str.replaceAll("\\s+", " "));
		scanner.useDelimiter(Pattern.compile("(\\s)"));
	}

	@Override
	public boolean hasNext() {
		return scanner.hasNext() || last != null;
	}

	@Override
	public String getLast() {
		if (last == null) {
			last = scanner.next();
		}
		return last;
	}

	@Override
	public String consume() throws ParserException {
		String last1 = getLast();
		logger.debug("consuming [" + last + "]");
		last = null;
		return last1;
	}

	// private Pattern NEW_LINE_PATTERN = Pattern.compile("(\\r\\n?|\\n)");

	@Override
	public boolean hasNewLine() {
		return scanner.hasNextLine();
	}

	@Override
	public String consumeNewLine() {
		return scanner.nextLine();
	}
}
